package com.hkemmm.control;

import com.hkemmm.bean.UserInfo;
import com.hkemmm.db.DBProvider;
import com.hkemmm.model.UserModel;
import com.hkemmm.utils.MyUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "UserInfoAddServlet", value = "/UserInfoAddServlet")
public class UserInfoAddServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //通过工具类获得一个对象
        UserInfo userInfo = MyUtils.getInstence(UserInfo.class);

        if(request.getParameter("userid") != null){
            userInfo.setUserid(Integer.valueOf(request.getParameter("userid")));
        }

        userInfo.setUserName(request.getParameter("userName"));
        userInfo.setUserPsw(request.getParameter("userPsw"));
        userInfo.setUserRegisteDate(MyUtils.coverStrToDate(
                request.getParameter("userRegisteDate"), "yyyy-MM-dd"));


        //boolean b = MyUtils.getInstence(UserModel.class).userNameIsExist(userInfo.getUserName());
        int i = MyUtils.getInstence(UserModel.class).saveUserInfo(userInfo);

        if (i >= 1) {
            List<UserInfo> list = MyUtils.getInstence(UserModel.class).queryUserInfoAll();
            request.getSession().setAttribute("listBean", list);


            //添加成功，跳转成功页面
            request.getRequestDispatcher("WEB-INF/viewpages/userInfoAddDetail.jsp").forward(request, response);
        } else {
            //添加失败，返回添加页面

            request.getRequestDispatcher("WEB-INF/viewpages/userInfoAddDetail1.jsp").forward(request, response);
        }

    }
}
